<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Han">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/try6.gitee.io/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/try6.gitee.io/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/try6.gitee.io/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/try6.gitee.io/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/try6.gitee.io/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/try6.gitee.io/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/try6.gitee.io/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="电商类网站不可避免的需要很多商品分类，为了使用户能够快速定位到想要的商品分类，一个详细的主页导航就显得举足轻重。 淘宝和京东等体量较大的电商平台，web端的主页都使用了分类详细的大导航，而且在导航的用户体验上做足了文章。 不知道大家有没有注意过他们在导航的实现上，和一般的导航有什么区别。 先埋个彩蛋 下面我们来从零开始模拟开发一个具有二级目录大导航。">
<meta property="og:type" content="article">
<meta property="og:title" content="京东大导航——预判用户行为（一）">
<meta property="og:url" content="http://try66.gitee.io/try6.gitee.io/2017/12/07/2017-12-07京东大导航——预判用户行为（一）/index.html">
<meta property="og:site_name" content="Hello JS">
<meta property="og:description" content="电商类网站不可避免的需要很多商品分类，为了使用户能够快速定位到想要的商品分类，一个详细的主页导航就显得举足轻重。 淘宝和京东等体量较大的电商平台，web端的主页都使用了分类详细的大导航，而且在导航的用户体验上做足了文章。 不知道大家有没有注意过他们在导航的实现上，和一般的导航有什么区别。 先埋个彩蛋 下面我们来从零开始模拟开发一个具有二级目录大导航。">
<meta property="og:locale" content="zh-Han">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2865721-dbfa4ca0dcd95782.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2865721-ebe8bffcde131404.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2865721-657e83fa6c039d98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/2865721-fa74fdb09d47795a.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-16T13:11:30.321Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="京东大导航——预判用户行为（一）">
<meta name="twitter:description" content="电商类网站不可避免的需要很多商品分类，为了使用户能够快速定位到想要的商品分类，一个详细的主页导航就显得举足轻重。 淘宝和京东等体量较大的电商平台，web端的主页都使用了分类详细的大导航，而且在导航的用户体验上做足了文章。 不知道大家有没有注意过他们在导航的实现上，和一般的导航有什么区别。 先埋个彩蛋 下面我们来从零开始模拟开发一个具有二级目录大导航。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/2865721-dbfa4ca0dcd95782.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/try6.gitee.io/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://try66.gitee.io/try6.gitee.io/2017/12/07/2017-12-07京东大导航——预判用户行为（一）/"/>





  <title>京东大导航——预判用户行为（一） | Hello JS</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cc7e3057efb482b0f56c29d2716384da";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Han">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/try6.gitee.io/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hello JS</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">所有的安排都是最好的</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/try6.gitee.io/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/try6.gitee.io/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://try66.gitee.io/try6.gitee.io/try6.gitee.io/2017/12/07/2017-12-07京东大导航——预判用户行为（一）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Try_Luies">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/try6.gitee.io/images/pic.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hello JS">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">京东大导航——预判用户行为（一）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-12-07T21:08:08+08:00">
                2017-12-07
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/try6.gitee.io/2017/12/07/2017-12-07京东大导航——预判用户行为（一）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2017/12/07/2017-12-07京东大导航——预判用户行为（一）/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/try6.gitee.io/2017/12/07/2017-12-07京东大导航——预判用户行为（一）/" class="leancloud_visitors" data-flag-title="京东大导航——预判用户行为（一）">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">Visitors&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="http://upload-images.jianshu.io/upload_images/2865721-dbfa4ca0dcd95782.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="nav.png"></p>
<p>电商类网站不可避免的需要很多商品分类，为了使用户能够快速定位到想要的商品分类，一个详细的主页导航就显得举足轻重。</p>
<p>淘宝和京东等体量较大的电商平台，web端的主页都使用了分类详细的大导航，而且在导航的用户体验上做足了文章。</p>
<p>不知道大家有没有注意过他们在导航的实现上，和一般的导航有什么区别。</p>
<p>先埋个彩蛋</p>
<p>下面我们来从零开始模拟开发一个具有二级目录大导航。<br><a id="more"></a></p>
<h4 id="html和css"><a href="#html和css" class="headerlink" title="html和css"></a>html和css</h4><p>常规的dom结构和样式实现</p>
<p>对于子导航的实现，淘宝使用了div的层层嵌套，而京东则是使用表格的结构。两种方式都能实现最终效果。可自行选择。</p>
<p>我在这里做了表格结构实现子导航的尝试。 </p>
<p>不过table结构比div更耗性能啦~<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class=&quot;sub&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;sub-content&quot; id=&apos;sub-a&apos;&gt;</span><br><span class="line">        &lt;ul &gt;</span><br><span class="line">            &lt;li&gt;&lt;a href=&quot;# &quot;&gt;家电馆&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;</span><br><span class="line">            ...</span><br><span class="line">        &lt;/ul&gt;</span><br><span class="line">        &lt;dl &gt;</span><br><span class="line">            &lt;dt&gt;&lt;a href=&quot;# &quot;&gt;电视&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/dt&gt;</span><br><span class="line">            &lt;dd&gt;&lt;a href=&quot;# &quot;&gt;曲面电视&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/dd&gt;</span><br><span class="line">            ...</span><br><span class="line">        &lt;/dl&gt;</span><br><span class="line">        &lt;dl &gt;</span><br><span class="line">            &lt;dt&gt;&lt;a href=&quot;# &quot;&gt;空调&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/dt&gt;</span><br><span class="line">            &lt;dd&gt;&lt;a href=&quot;# &quot;&gt;曲面空调&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/a&gt;&lt;/dd&gt;</span><br><span class="line">            ...</span><br><span class="line">        &lt;/dl&gt;	</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure></p>
<p>为了方便后续使用js对样式的操作，在css中单独设置了名为none的类，设置隐藏样式<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.none&#123;</span><br><span class="line">    display:none</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="js部分"><a href="#js部分" class="headerlink" title="js部分"></a>js部分</h4><h6 id="step1：常规导航实现"><a href="#step1：常规导航实现" class="headerlink" title="step1：常规导航实现"></a>step1：常规导航实现</h6><h6 id="思路："><a href="#思路：" class="headerlink" title="思路："></a>思路：</h6><ul>
<li>鼠标移至主导航或子导航菜单中(mouseener)，对应选项样式改变，对应子导航显示</li>
<li>鼠标移出主导航或子导航菜单中(mouseleave)，选项样式取消，对应子导航隐藏<blockquote>
<p>注：mouseener不冒泡，mouseover冒泡。详细内容请戳<a href="">mouseover,mouseener,mousemove</a></p>
</blockquote>
</li>
</ul>
<h6 id="效果gif如下："><a href="#效果gif如下：" class="headerlink" title="效果gif如下："></a>效果gif如下：</h6><p><img src="http://upload-images.jianshu.io/upload_images/2865721-ebe8bffcde131404.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="step1.gif"></p>
<p>一般实现到这一步，已经有了基本功能，可以流畅地进行导航切换。很多网站也只实现到了这一步，但事实上，这个导航还存在一些问题，有继续优化的空间。</p>
<h6 id="存在问题："><a href="#存在问题：" class="headerlink" title="存在问题："></a>存在问题：</h6><ol>
<li>用户想到要选择子导航的某一选项时，鼠标必须先进入子导航内。如果在此之前触碰到主导航的其他选项，会立即切换。导航体量大，如果用户想要选择子导航内下方的选项，必须先从主导航平移至子导航内，也就是一个折线的过程。</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/2865721-657e83fa6c039d98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="nav_1.png"></p>
<h6 id="step2-延时切换"><a href="#step2-延时切换" class="headerlink" title="step2.延时切换"></a>step2.延时切换</h6><p>针对第一个问题，我们采取setTimeout,在mouseener事件被触发后，让子导航显示代码并不马上执行，而是延迟一段时间后再执行。并且每次执行前先判断鼠标是否在子菜单内，若不在则不执行切换导航代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">var mouseInSub = false;</span><br><span class="line">sub-nav.on(&quot;mouseenner&quot;,function()&#123;</span><br><span class="line">    mouseInSub = true;</span><br><span class="line">&#125;);</span><br><span class="line">sub-nav.on(&quot;mouseleave&quot;,function()&#123;</span><br><span class="line">    mouseInSub = false;</span><br><span class="line">&#125;);</span><br><span class="line">var time = setTimeout(function() &#123;</span><br><span class="line">    if (mouseInSub) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    $(&quot;.sub-content&quot;).addClass(&quot;none&quot;);</span><br><span class="line">    subNav.removeClass(&quot;none&quot;);</span><br><span class="line">    setTimeout(function() &#123;</span><br><span class="line">        li.on(&quot;mouseleave&quot;, function() &#123;</span><br><span class="line">            let li = $(this);</span><br><span class="line">            li.removeClass(&apos;active&apos;);</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;, 500);</span><br></pre></td></tr></table></figure></p>
<p>很明显，采取这种方法就引入了一个新的问题：用户在主导航上快速切换时，子导航的出现存在延时，降低了用户体验的流畅度<br><img src="http://upload-images.jianshu.io/upload_images/2865721-fa74fdb09d47795a.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="step2.gif"></p>
<p>这就需要进一步进行优化。</p>
<h6 id="step3-用户行为预判"><a href="#step3-用户行为预判" class="headerlink" title="step3.用户行为预判"></a>step3.用户行为预判</h6><p>思路：对用户的鼠标移动行为作出判断，若用户是想要移动到子导航，则进行延迟；若用户想在主导航上移动，则不需要延迟</p>
<p>如何判断用户到底是想要移动到子导航，还是想在主导航上移动呢？</p>
<p>详细内容戳<a href="http://www.jianshu.com/p/cc37f42a0b13" target="_blank" rel="noopener">京东大导航——预判用户行为（二）</a></p>
<p>源码见<a href="https://github.com/try6/nav-Solution" target="_blank" rel="noopener">个人github地址</a>!</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>Donate</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Try_Luies WeChat Pay"/>
        <p>WeChat Pay</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Try_Luies Alipay"/>
        <p>Alipay</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/try6.gitee.io/2017/09/28/2017-09-28移动端触摸分页方案-1/" rel="next" title="移动端触摸分页方案">
                <i class="fa fa-chevron-left"></i> 移动端触摸分页方案
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/try6.gitee.io/2017/12/08/2017-12-08京东大导航——预判用户行为（二）/" rel="prev" title="京东大导航——预判用户行为（二）">
                京东大导航——预判用户行为（二） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/try6.gitee.io/images/pic.jpg"
                alt="Try_Luies" />
            
              <p class="site-author-name" itemprop="name">Try_Luies</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/try6.gitee.io/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/try6" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/7a6944daafc3" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-globe"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="www.baidu.com" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-微博"></i>微博</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#html和css"><span class="nav-number">1.</span> <span class="nav-text">html和css</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#js部分"><span class="nav-number">2.</span> <span class="nav-text">js部分</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#step1：常规导航实现"><span class="nav-number">2.0.1.</span> <span class="nav-text">step1：常规导航实现</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#思路："><span class="nav-number">2.0.2.</span> <span class="nav-text">思路：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#效果gif如下："><span class="nav-number">2.0.3.</span> <span class="nav-text">效果gif如下：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#存在问题："><span class="nav-number">2.0.4.</span> <span class="nav-text">存在问题：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#step2-延时切换"><span class="nav-number">2.0.5.</span> <span class="nav-text">step2.延时切换</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#step3-用户行为预判"><span class="nav-number">2.0.6.</span> <span class="nav-text">step3.用户行为预判</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Try_Luies</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/try6.gitee.io/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/try6.gitee.io/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/try6.gitee.io/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/try6.gitee.io/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/try6.gitee.io/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/try6.gitee.io/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://try.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://try66.gitee.io/try6.gitee.io/2017/12/07/2017-12-07京东大导航——预判用户行为（一）/';
          this.page.identifier = '2017/12/07/2017-12-07京东大导航——预判用户行为（一）/';
          this.page.title = '京东大导航——预判用户行为（一）';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://try.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("", "");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  

  

  

</body>
</html>
